home *** CD-ROM | disk | FTP | other *** search
/ MacFormat 1995 July / macformat-026.iso / mac / Shareware City / Developers / WASTE Object Handlers v1.0 ƒ / Sample Code / DragTextFile.c
Encoding:
Text File  |  1995-03-20  |  4.4 KB  |  168 lines  |  [TEXT/MPCC]

  1. /*
  2.  
  3. This is an example of how to drag text files to a WASTE instance and
  4. have them imported.  For this example to work, you must be using the 
  5. 'hfs ' type WASTE object supplied with this package.
  6.  
  7. Michael F. Kamprath
  8. kamprath@earthlink.net
  9. 19 March 1995
  10. */
  11.  
  12. pascal OSErr    MyWEReceiveHandler(    WindowPtr theWindow,
  13.                             Ptr handlerRefCon,
  14.                             DragReference theDrag)
  15. {
  16. ItemReference    theItem;
  17. Size            itemSize;
  18. Ptr                textP;
  19. StScrpHandle    styleH;
  20. WESoupHandle    soupH;
  21. GrafPtr            savePort;
  22. DocPtr            theDoc;
  23. HFSFlavor        **theHFS;
  24. long            insertionOffset, oldNLines, curEOF;
  25. unsigned short    items, index;
  26. short            fRefNum;    
  27. OSErr            iErr;
  28. Point             zeroPoint = {0, 0};
  29. Point             mouse;
  30. LongPt             dropLocation;
  31. char             dropEdge;
  32.  
  33.     if (theWindow != nil)
  34.     {
  35.         GetPort(&savePort);
  36.         SetPort(theWindow);
  37.  
  38.         theDoc = (DocPtr)GetWRefCon( theWindow );     // Or recover your document or
  39.                                                     // WASTE handle how ever you see 
  40.                                                     // fit.
  41.         
  42.         oldNLines = WECountLines( theDoc->theWE );
  43.  
  44.         CountDragItems(theDrag, &items);
  45.  
  46.         for (index = 1; index <= items; index++)     // step through each drag item
  47.                                                     // and check for text files
  48.         {
  49.             GetDragItemReferenceNumber(theDrag, index, &theItem);
  50.                 
  51.             if (!(iErr = GetFlavorDataSize(theDrag, theItem, flavorTypeHFS, &itemSize)))
  52.             {
  53.                                                     // Found a text file.  Now import
  54.                                                     // it to the WASTE instance;
  55.                                                     
  56.                 theHFS = (HFSFlavor **)NewHandleClear(sizeof(HFSFlavor));
  57.             
  58.                 if (theHFS)
  59.                 {
  60.                     HLockHi( (Handle)theHFS );
  61.                     GetFlavorData( theDrag, theItem, flavorTypeHFS, *theHFS, &itemSize, 0L);
  62.                     
  63.                     if (( (*theHFS)->fileType == 'TEXT' )||( (*theHFS)->fileType == 'ttro' ))
  64.                     {
  65.                         // Load the text file into memory
  66.                         
  67.                         iErr = FSpOpenDF( &(*theHFS)->fileSpec, fsCurPerm, &fRefNum );
  68.                         iErr = GetEOF(fRefNum,&curEOF);
  69.                         
  70.                         textP = NewPtrClear( curEOF );
  71.     
  72.                         iErr = SetFPos(fRefNum,fsFromStart,0);
  73.                         iErr = FSRead( fRefNum, &curEOF, textP);
  74.                         iErr = FSClose( fRefNum );
  75.     
  76.                         fRefNum = FSpOpenResFile(&(*theHFS)->fileSpec,fsCurPerm);
  77.     
  78.                         if ( fRefNum != -1 )
  79.                         {
  80.                             // has resource fork, so look for style and soup
  81.                             
  82.                             styleH = (StScrpHandle)Get1Resource( 'styl', 128 );
  83.                             if (iErr = ResError())
  84.                                 styleH = nil;
  85.                             else
  86.                                 DetachResource( (Handle)styleH );
  87.  
  88.                             soupH = (WESoupHandle)Get1Resource( 'soup', 128 );
  89.                             if (iErr = ResError())
  90.                                 soupH = nil;
  91.                             else
  92.                                 DetachResource( (Handle)soupH );
  93.  
  94.         
  95.                             CloseResFile(fRefNum);
  96.                         }
  97.                         else    
  98.                         {
  99.                             // does not have resource fork, so no style or soup.
  100.                             
  101.                             styleH = nil;
  102.                             soupH = nil;
  103.                         }
  104.     
  105.                         // insert the text file into the current position
  106.                         
  107.                         iErr = GetDragMouse(theDrag, &mouse, &zeroPoint);
  108.                         if (iErr != noErr)
  109.                             goto cleanup;
  110.                         GlobalToLocal(&mouse);
  111.  
  112.                         WEPointToLongPoint(mouse, &dropLocation);
  113.                         insertionOffset = WEGetOffset(&dropLocation, &dropEdge, theDoc->theWE);
  114.                         WESetSelection( insertionOffset, insertionOffset, theDoc->theWE );
  115.                              
  116.                         iErr = WEInsert(textP, curEOF, styleH, soupH, theDoc->theWE);
  117.                             
  118.                         WESetSelection( insertionOffset, insertionOffset+curEOF, theDoc->theWE );
  119.                             
  120.                         // dispose of memory
  121. cleanup:                        
  122.                         HUnlock( (Handle)theHFS );
  123.                         DisposHandle( (Handle)theHFS );
  124.                         DisposPtr( textP );
  125.                         if (styleH) DisposHandle( (Handle)styleH );
  126.                         if (soupH) DisposHandle( (Handle)soupH );
  127.                         
  128.                         // update the WASTE instance if need be
  129.                         //
  130.                         // your program will probably do ssomething different
  131.                         
  132.                         if (  WECountLines( theDoc->theWE ) != oldNLines )
  133.                             SetVScroll(theDoc);
  134.                         theDoc->dirty = true;
  135.                         
  136.                         SetPort(savePort);
  137.                         return (iErr);
  138.                     }
  139.                     HUnlock( (Handle)theHFS );
  140.                     DisposHandle( (Handle)theHFS );
  141.                 }
  142.             }
  143.             
  144.             // If the code got to here, that means there was no
  145.             // text file item in the drag.  Let WASTE handle the drag then.
  146.             
  147.             HideDragHilite(theDrag);  // I found I need to do this for cosmetic purposes sometimes
  148.             
  149.             iErr = WEReceiveDrag(theDrag, theDoc->theWE );
  150.             
  151.             // update the WASTE instance if need be
  152.             //
  153.             // your program will probably do ssomething different
  154.             if (  WECountLines( theDoc->theWE ) != oldNLines )
  155.                 SetVScroll(theDoc);
  156.             theDoc->dirty = true;
  157.         }
  158.         else
  159.             iErr = dragNotAcceptedErr );
  160.  
  161.         SetPort(savePort);
  162.     }
  163.     else
  164.         iErr = noErr;
  165.     
  166.     return(noErr);
  167. }
  168.